package com.offcn.web;

import com.fasterxml.jackson.databind.ObjectMapper;


import com.offcn.entity.User;
import com.offcn.service.CourseService;
import com.offcn.service.CourseServiceImpl;
import com.offcn.service.UserService;
import com.offcn.service.UserServiceImpl;
import com.offcn.utils.BaseServlet;
import com.offcn.utils.PageUtils;
import com.offcn.utils.ResultVO;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * 用户类Servlet
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet {
    //实例化service
    UserService userService = new UserServiceImpl();
    //创建响应数据工具类
    ResultVO resultVO = null;
    //创建转换数据类型工具类对象
    ObjectMapper objectMapper = new ObjectMapper();
    /**
     * 登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取登录界面参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String imageCode = req.getParameter("imageCode");
        //获取session作用域
        HttpSession session = req.getSession();
        //获取验证码
        String verCode = (String) session.getAttribute("verCode");
        if(verCode.equalsIgnoreCase(imageCode)){
            //如果验证码正确  调用login判断用户名密码是否正确
            User user = userService.login(username,password);
            if(user != null){
                //如果正确 判断权限是否足够 1为管理员
                if(user.getRole()==1){
                    //把状态封装到ResultVO实例中
                    resultVO = new ResultVO(4,"登录成功",user);
                    //添加到session用于验证是否有登录
                    session.setAttribute("user",user);
                }else {
                    //role为1以外权限不足
                    resultVO = new ResultVO(3,"权限不足",null);
                }
            }else {
                //未查到用户名或密码
                resultVO = new ResultVO(2,"用户名或密码错误",null);
            }
        }else {
            //验证码输入不一致
            resultVO = new ResultVO(1, "状态码有误", null);
        }
        //转换为json格式
        String json = objectMapper.writeValueAsString(resultVO);
        //输出响应
        resp.getWriter().println(json);
    }

    /**
     * 获取用户信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void getUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        //获取session作用域存储的信息
        Object user = session.getAttribute("user");
            resultVO = new ResultVO(200, "获取用户信息成功", user);
        //转换为json格式
        String json = objectMapper.writeValueAsString(resultVO);
        //输出响应
        resp.getWriter().println(json);
    }

    /**
     * 退出登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        //移除session
        session.removeAttribute("user");
        //反馈
        resultVO = new ResultVO(200, "退出登录成功", null);
        //转换为json格式
        String json = objectMapper.writeValueAsString(resultVO);
        //输出响应
        resp.getWriter().println(json);
    }

    /**
     * 添加用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        User user = new User();
        //使用BeanUtils工具获取参数集合
        BeanUtils.populate(user,req.getParameterMap());
        //调用service层判断是否成功
        boolean judge = userService.addUser(user);
        if(judge){
            //结果为true反馈成功
            resultVO = new ResultVO(200,"添加成功",null);
        }else {
            //结果为true反馈失败
            resultVO = new ResultVO(500,"添加失败",null);
        }
        //转换为json格式
        String json = objectMapper.writeValueAsString(resultVO);
        //输出响应
        resp.getWriter().println(json);
    }

    /**
     * 分页查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //获取参数
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        String search = req.getParameter("search");
        //调用service层查询
        PageUtils<User> pageUtils = userService.findByPage(currentPage,pageSize,search);
        if(pageUtils != null){
            resultVO = new ResultVO(200,"查询成功",pageUtils);
        }else {
            resultVO = new ResultVO(500,"查询失败",null);
        }
        //转换为json格式
        String json = objectMapper.writeValueAsString(resultVO);
        //输出响应
        resp.getWriter().println(json);
    }

    /**
     * 修改用户信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public void updateUserById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        User user = new User();
        //获取参数
        BeanUtils.populate(user,req.getParameterMap());
        //调用service层方法修改方法
        if(userService.updateUserById(user)){
            resultVO = new ResultVO(200,"修改成功",null);
        }else {
            resultVO = new ResultVO(500,"修改失败",null);
        }
        //响应数据
        String json = objectMapper.writeValueAsString(resultVO);
        resp.getWriter().println(json);
    }

    /**
     * 批量删除
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void delAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String[] uids = req.getParameterValues("uid");
        //调用service层删除方法
        if(userService.delAll(uids)){
            resultVO = new ResultVO(200,"删除成功",null);
        }else {
            resultVO = new ResultVO(500,"删除失败",null);
        }
        //响应数据
        String json = objectMapper.writeValueAsString(resultVO);
        resp.getWriter().println(json);
    }
}


